home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 July / Macworld (1999-07).dmg / Serious Software / OpenWorld demo 2.0 / Development / SingleTag / Counter / Counter.c next >
Text File  |  1999-04-28  |  4KB  |  216 lines

  1. /*  File: Counter.c      
  2.     Copyright (c) 1999 Marco Bambini. All rights reserved.
  3.  
  4.     Description: page counter
  5. */
  6.  
  7. #include "Counter.h"
  8.  
  9. // global variables
  10. static long    times=0;
  11.  
  12. Boolean main (dataPtr param)
  13. {    
  14.     switch (param->message)
  15.     {    
  16.         case init_message:
  17.             {    
  18.                 FILE        *f;
  19.                 HeadQueue    *q;
  20.                 
  21.                 // create the queue
  22.                 create_queue(q);
  23.                 if (q==NULL) return NULL;
  24.                 
  25.                 // initialize fields
  26.                 param->version=kVersion;
  27.                 param->customData=(long)q;
  28.                 
  29.                 // try to open the Counter file
  30.                 f=fopen(kFileName,"r");
  31.                 if (f)
  32.                     {    // the file exists
  33.                         char    line[kMaxLine];    // max characters in a line
  34.                         int        max=kMaxLine;
  35.                         char    *tok;
  36.                         long    n;
  37.                         
  38.                         // read the file (one line at a time) and add it to the list
  39.                         while (fgets(line,max,f))
  40.                         {
  41.                                 tok=strtok(line,"\t ");
  42.                                 n=atol(strtok(NULL,"\n "));
  43.                                 my_add_queue(param,tok,n);
  44.                         }
  45.                     }
  46.                 else
  47.                     {
  48.                         // create the file
  49.                         f=fopen(kFileName,"w");
  50.                     }
  51.                 
  52.                 // close the file
  53.                 fclose(f);
  54.             }
  55.             break;
  56.             
  57.         case run_message:
  58.             {    
  59.                 obj        *item;
  60.                 long    l;
  61.                         
  62.                 if (param->search==NULL) return NULL;
  63.                 if (param->customData==NULL) return NULL;
  64.                 
  65.                 item=find_queue((HeadQueue *)param->customData,param->search);
  66.                 
  67.                 if (item)
  68.                     {    // there is the item in the list, so return its value+1
  69.                         item->value+=1;
  70.                         l=item->value;
  71.                     }
  72.                     else
  73.                     {    // new item, add to the list and return 1
  74.                         l=1;
  75.                         my_add_queue(param,param->search,l);
  76.                     }
  77.                 
  78.                 // prepare the output
  79.                 param->outputText=(char *)OW_NewPtr(param,12);
  80.                 if (param->outputText) param->dim=sprintf(param->outputText,"%ld",l);
  81.             }
  82.             break;
  83.         
  84.         case stop_message:
  85.             {
  86.                 // dispose the buffer
  87.                 OW_DisposePtr(param,param->outputText);
  88.                 
  89.                 // increment the global counter
  90.                 ++times;
  91.                 
  92.                 // check if I have to write the list to the file
  93.                 if (times==kMaxTimes) {WriteListToFile((HeadQueue *)param->customData);times=0;}
  94.             }
  95.             break;
  96.         
  97.         case end_message:
  98.             {    
  99.                 // write the list to the file
  100.                 WriteListToFile((HeadQueue *)param->customData);
  101.                 
  102.                 // delete the queue
  103.                 delete_queue(param,(HeadQueue *)param->customData);
  104.             }
  105.             break;
  106.     }
  107.     return kSingleTag;
  108. }
  109.  
  110. void WriteListToFile(HeadQueue *q)
  111. {    
  112.     FILE    *f;
  113.     QueueEl    *now;
  114.     char    line[kMaxLine];    // max characters in a line
  115.     
  116.     // open the file
  117.     f=fopen(kFileName,"w");
  118.     if (f==NULL) return;
  119.     
  120.     now=q->first;
  121.     
  122.     while (now!=NULL)
  123.         {    
  124.             sprintf(line,"%s\t%ld\n",now->item->page,now->item->value);
  125.             fputs(line,f);
  126.             
  127.             now=now->next;
  128.         }
  129.     
  130.     // close the file
  131.     fclose(f);
  132. }
  133.  
  134. void my_add_queue(dataPtr param,char *s,long l)
  135. {
  136.     obj    *item=NULL;
  137.     
  138.     // create a new object
  139.     item=(obj *)OW_NewPtr(param,sizeof(obj));
  140.     if (item==NULL) return;
  141.     
  142.     // set its fields
  143.     item->page=(char *)OW_NewPtr(param,strlen(s)+1);
  144.     if (item->page) strcpy(item->page,s);
  145.     item->value=l;
  146.     
  147.     add_queue(param,(HeadQueue    *)param->customData,item);
  148. }
  149.  
  150. /**************************************************************/
  151. /*  File: llist.c (linked list routines)
  152.          
  153.     Copyright (c) 1997-99 Marco Bambini. All rights reserved.
  154.  
  155.     Description:
  156.  
  157.     Linked list manager.
  158.     
  159.       Revision history:
  160.       970613    Created.  (MB)
  161.       981020  revised to use with WSAPI
  162.       990427    revised to use with OpenWorld API
  163. */
  164. /**************************************************************/
  165.  
  166. void add_queue(dataPtr param,HeadQueue *phq, void *item)
  167. {
  168.     QueueEl    *pqe;
  169.     
  170.     pqe=(QueueEl *)OW_NewPtr(param,sizeof(QueueEl));
  171.     pqe->next=NULL;
  172.     pqe->item=item;
  173.     
  174.     if (phq->cont==0)
  175.     {
  176.         phq->first=pqe;
  177.         phq->last=pqe;
  178.     }
  179.     else
  180.     {
  181.         phq->last->next=pqe;
  182.         phq->last=pqe;
  183.     }
  184.     phq->cont=phq->cont+1;
  185. }
  186.  
  187. void delete_queue(dataPtr param,HeadQueue *phq)
  188. {
  189.     QueueEl *prec,*now;
  190.     
  191.     now=phq->first;
  192.     while (now!=NULL)
  193.         {
  194.             prec=now->next;
  195.             if (now->item->page) OW_DisposePtr(param,(Ptr)now->item->page);
  196.             if (now->item) OW_DisposePtr(param,(Ptr)now->item);
  197.             if (now) OW_DisposePtr(param,(Ptr)now);
  198.             now=prec;
  199.         }
  200.     phq->cont=0;
  201. }
  202.  
  203. obj    *find_queue(HeadQueue *phq,char *tag)
  204. {
  205.     QueueEl *now;
  206.     obj        *last=NULL;
  207.     
  208.     now=phq->first;
  209.     while (now!=NULL)
  210.         {    
  211.             if (up_strcmp(now->item->page,tag)==NULL) return now->item;
  212.             now=now->next;
  213.         }// while (now!=NULL)
  214.     return NULL;
  215. }
  216.